﻿using SqlSugar;
using System;
using Utils;

namespace _AutoGen
{
    public static class Tools
    {
        /// <summary>
        /// 字符串处理  (_切分，首字母大写 （.)去除  )    ----》 扩展方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToCase(this string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            string[] arry = value.Split('_');
            string str = "";
            foreach (string item in arry)
            {
                string newstr = item.Replace("(", "").Replace(".", "").Replace(")", "");
                string firstLetter = newstr[..1];
                string rest = newstr[1..];
                str += firstLetter.ToUpper() + rest.ToLower();
            }
            return str;
        }
    }

    internal class Program
    {
        static void Main()
        {
            //AutoGen();
            Test();
        }

        static void Test()
        {
            string salt = Guid.NewGuid().ToString();
            var cryptedPwd = EncryptHelper.Encrypt("123456", EncryptHelper.ALGORITHM_SHA256, salt);
            Console.WriteLine(salt);
            Console.WriteLine(cryptedPwd);
        }

        static void AutoGen()
        {
            string nameSpanceInfo = "BlazorApp.Model";


            using var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=localhost;uid=testuser;pwd=330821;Database=testdb;SslMode=none;",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            });
            foreach (var table in db.DbMaintenance.GetTableInfoList())
            {
                string entityName = Tools.ToCase(table.Name);
                db.MappingTables.Add(entityName, table.Name);
                foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(table.Name))
                {
                    db.MappingColumns.Add(Tools.ToCase(col.DbColumnName), col.DbColumnName, entityName);
                }
                db.DbFirst.IsCreateAttribute().Where(table.Name).CreateClassFile(@"..\..\..\..\Model", nameSpanceInfo);
            }
        }
    }
}